package com.edie.customview.basemvp;

import java.lang.ref.WeakReference;

/**
 * edie create on 2018/7/19
 */
public class BasePresenter<V extends IView> implements IPresenter<V> {
    protected WeakReference<V> mViewRf;

    public BasePresenter(V view) {
      onAttach(view);
    }

    @Override
    public void onAttach(V view) {
        mViewRf = new WeakReference<>(view);
    }

    @Override
    public void onDetach() {
        if (mViewRf != null) {
            mViewRf.clear();
            mViewRf = null;
        }
    }

    /**
     * view 是否存在
     * @return
     */
    public boolean isAttach() {
        return mViewRf != null && mViewRf.get() != null;
    }

    /**
     * 应该在调用{@link #isAttach()}判断后使用
     * @return 返回view
     */
    public V getView() {
        return mViewRf.get();
    }

}
